﻿using System.ComponentModel;
using System.Threading.Tasks;
using Loki.Utils;

namespace Loki.UI
{
    public abstract class SplashViewModel : ViewModelBase
    {
        #region Message

        private static PropertyChangedEventArgs argsMessageChanged = ObservableHelper.CreateChangedArgs<SplashViewModel>(x => x.Message);

        private static PropertyChangingEventArgs argsMessageChanging = ObservableHelper.CreateChangingArgs<SplashViewModel>(x => x.Message);

        private string message;

        public string Message
        {
            get
            {
                return message;
            }

            set
            {
                if (value != message)
                {
                    NotifyChanging(argsMessageChanging);
                    message = value;
                    NotifyChanged(argsMessageChanged);
                }
            }
        }

        #endregion Message

        #region Percentage

        private static PropertyChangedEventArgs argsPercentageChanged = ObservableHelper.CreateChangedArgs<SplashViewModel>(x => x.Percentage);

        private static PropertyChangingEventArgs argsPercentageChanging = ObservableHelper.CreateChangingArgs<SplashViewModel>(x => x.Percentage);

        private double? percentage;

        public double? Percentage
        {
            get
            {
                return percentage;
            }

            set
            {
                if (value != percentage)
                {
                    NotifyChanging(argsPercentageChanging);
                    percentage = value;
                    NotifyChanged(argsPercentageChanged);
                }
            }
        }

        #endregion Percentage

        public bool StartLoading()
        {
            bool buffer = false;
            try
            {
                var result = Task.Factory.StartNew<bool>(LoadingProcess);
                result.Wait();
                buffer = result.Result;
            }
            finally
            {
            }

            return buffer;
        }

        protected abstract bool LoadingProcess();
    }
}